﻿using lauimini_webform.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using lauimini_webform.Data;
using Newtonsoft.Json;
using System.Web.Services;
using mydesign.Models;

namespace lauimini_webform.BLL
{
    public class InitMenu
    {
        MyDBContext db;
        public InitMenu()
        {
            db = new MyDBContext();
            //db.Database.EnsureCreated();
            //db.SystemMenuEntity.Add(new SystemMenuEntity() { title = "日程表", href = "/plan.aspx", target = "_self" });
            //db.SystemMenuEntity.Add(new SystemMenuEntity() { title = "正在进行", href = "/doing.aspx", target = "_self" });
            //db.SystemMenuEntity.Add(new SystemMenuEntity() { title = "统计分析", href = "/analysis.aspx", target = "_self" });
            //db.SystemMenuEntity.Add(new SystemMenuEntity() { title = "成就", href = "/achievement.aspx", target = "_self" });
            //db.SystemMenuEntity.Add(new SystemMenuEntity() { title = "我的", href = "/mine.aspx", target = "_self" });

            //Accomplishment[] accomplishments = new Accomplishment[] 
            //{
            //    new Accomplishment(){description="黑铁",detail="完成一个任务" },
            //    new Accomplishment(){description="青铜",detail="完成二个任务" },
            //    new Accomplishment(){description="白银",detail="完成三个任务" },
            //    new Accomplishment(){description="铂金",detail="完成四个任务" },
            //    new Accomplishment(){description="钻石",detail="完成五个任务" },
            //    new Accomplishment(){description="星耀",detail="完成六个任务" },
            //    new Accomplishment(){description="王者",detail="完成七个任务" },
            //};
            //db.Accomplishment.AddRange(accomplishments);
            //db.SaveChanges();

        }
        [WebMethod]
        public string initData()
        {
            List<SystemMenuEntity> systemMenuEntities = new List<SystemMenuEntity>();
            systemMenuEntities = db.SystemMenuEntity.Where(s => s.id > 0).ToList();
            SystemMenu rootNode = new SystemMenu()
            {
                id = 0,
                icon = "",
                href = "",
                title = "根目录",
            };

            GetTreeNodeListByNoLockedDTOArray(systemMenuEntities.ToArray(), rootNode);
            MenusInfoResultDTO menusInfoResultDTO = new MenusInfoResultDTO();
            menusInfoResultDTO.menuInfo = rootNode.Child;
            menusInfoResultDTO.logoInfo = new LogoInfo();
            menusInfoResultDTO.homeInfo = new HomeInfo();

            return JsonConvert.SerializeObject(menusInfoResultDTO);
        }

        /// <summary>
        /// 递归处理数据
        /// </summary>
        /// <param name="systemMenuEntities"></param>
        /// <param name="rootNode"></param>
        public void GetTreeNodeListByNoLockedDTOArray(SystemMenuEntity[] systemMenuEntities, SystemMenu rootNode)
        {
            if (systemMenuEntities == null || systemMenuEntities.Count() <= 0)
            {
                return;
            }

            var childreDataList = systemMenuEntities.Where(p => p.pid == rootNode.id);
            if (childreDataList != null && childreDataList.Count() > 0)
            {
                rootNode.Child = new List<SystemMenu>();

                foreach (var item in childreDataList)
                {
                    SystemMenu treeNode = new SystemMenu()
                    {
                        id = item.id,
                        icon = item.icon,
                        href = item.href,
                        title = item.title,
                    };
                    rootNode.Child.Add(treeNode);
                }

                foreach (var item in rootNode.Child)
                {
                    GetTreeNodeListByNoLockedDTOArray(systemMenuEntities, item);
                }
            }



        }
    }
}